﻿using System;
using System.Collections.Generic;
using System.Text;
using NDde.Client;
using System.Threading;

namespace BasicMachine
{
    public partial class Form1
    {
        private DdeClient dclient2;
        public int m_NextMonthIndexValue;
        public int m_OpeningValue2 = 0;
        public bool m_Alive2 = true;

        private void CheckLive2()
        {
            TimeSpan endTime = new TimeSpan(13, 45, 0);
            TimeSpan startTime = new TimeSpan(8, 45, 0);

            while (true)
            {
                if (DateTime.Now.TimeOfDay >= endTime || DateTime.Now.TimeOfDay <= startTime)
                    m_Alive2 = true;
                else if (m_Alive2)
                    m_Alive2 = false;
                else
                {
                    if (!m_sent && DateTime.Now.Date.DayOfWeek != DayOfWeek.Saturday)
                        SendAert("DDE SERVER DISCONNECTED");

                    m_sent = true;
                }
                Thread.Sleep(60000);
            }
        }


        private void StartReceive2()
        {
            bool excepBreak = false;

            string ddeService = "FUTURETXF" + ReturnCharMonth(lab_NextMonth.Text);
            dclient2 = new DdeClient("DDEEXCEL", ddeService, this);
            //dclient2 = new DdeClient("myapp", "myservice", this);
            dclient2.Advise += new EventHandler<DdeAdviseEventArgs>(dclient_Advise2);
            dclient2.Disconnected += new EventHandler<DdeDisconnectedEventArgs>(dclient_Disconnected2);

            Thread.Sleep(1000);

            string result;
            try
            {
                dclient2.Connect();
                dclient2.StartAdvise("市價", 1, true, 60000);

                result = "DDE Next Month Connection Successful";
                SendEmailAlert(result);
            }
            catch (Exception ex)
            {

                label_NextIndex.ForeColor = System.Drawing.Color.Red;
                label_NextIndex.Text = "DDE Next Month Connection Fail";
                result = label_NextIndex.Text;

                excepBreak = true;

                Thread.Sleep(10000);
                Thread ddeOps2 = new Thread(new ThreadStart(StartReceive2));
                ddeOps2.Start();
            }
            if (excepBreak && !m_failSend)
            {
                SendEmailAlert(result);
                m_failSend = true;
            }
        }


        void dclient_Disconnected2(object sender, DdeDisconnectedEventArgs e)
        {
            if (!m_sent)
            {
                SMSender.sendMessage("DDE Server 斷線", "DDE Server 斷線!! ", "", "0938920062");
                m_sent = true;
            }

            AlertMail.MailTo = m_SystemMailList;

            AlertMail.MailSubject = "DDE Server Disconnect !!";
            AlertMail.MailBody = DateTime.Now.TimeOfDay.ToString() + AlertMail.MailSubject;
            AlertMail.Send();

            label_NextIndex.Text = "DDE SERVER DISCONNECTED";
            Thread.Sleep(10000);
            Thread ddeOps = new Thread(new ThreadStart(StartReceive2));
            ddeOps.Start();
        }

        void dclient_Advise2(object sender, DdeAdviseEventArgs e)
        {
            label_NextIndex.ForeColor = System.Drawing.Color.MediumSeaGreen;
            m_NextMonthIndexValue = Convert.ToInt32(e.Text);
            label_NextIndex.Text = e.Text;

            m_sent = false;
            m_Alive2 = true;
        }
    }
}
